/**
 * 数字格式化
 * @param  {Number|String} numr 数字
 * @return {String}        格式化后的数字
 */
const formatNumber = function (num) {
  let result = ''
  let count = 0

  if (typeof num === 'number') {
    num = num.toString()
  }

  let numArr = num.split('.')
  let integer = numArr[0]
  let digit = numArr[1]

  for (let i = integer.length - 1; i >= 0; i--) {
    if (count % 3 === 0 && count !== 0) {
      result = `${integer.charAt(i)},${result}`
    } else {
      result = `${integer.charAt(i)}${result}`
    }
    count++
  }

  if (digit) {
    result = `${result}.${digit}`
  }

  return result
}

export default formatNumber
